import dayjs from 'dayjs'
// Todo 获取树中某一字段的详细信息
export const treeValue = (tree, value, key = 'uuid') => {
    for (let i = 0; i < tree.length; i++) {
        if (tree[i][key] === value) {
            return tree[i]
        } else if (tree[i].children && tree[i].children.length) {
            const result = treeValue(tree[i].children, value, key)
            if (result) return result
        }
    }
    return null
}
// Todo 获取对象数组中某一段的详细信息
export const objectArrValue = (arr, value, key = 'uuid') => {
    return arr.find(obj => obj[key] === value);
}
// Todo 判断某一个字段是否在对象数组中
export const objectArrHas = (arr, value, key = 'uuid') => {
    return arr.some(obj => obj[key] === value);
}

// Todo Form表单时间处理后函数(提交时触发)
export const formTempDateHandleBefore = (field, date) => {
    const { picker, showTime, format } = field
    if (picker) {
        const pickerMapping = { year: 'YYYY', month: 'YYYY-MM', date: 'YYYY-MM-DD' }
        return dayjs(date).format(pickerMapping[picker])
    } else if (format) {
        return new Date(dayjs(date).format(format.toUpperCase()))
    }
}

// Todo 字符换转换请求体对象
export const stringToObject = (str) => {
    const newStr = str ? str
        .split('&')
        .map(pair => pair
            .split('='))
        .map(([key, value]) => [decodeURIComponent(key), decodeURIComponent(value)])
        .reduce((obj, [key, value]) => {
            obj[key] = value
            return obj
        }, {}) : ''
    return newStr
}

// Todo Debug函数
export const debuggerFun = () => {
    const block = () => {
        // window.location.href = window.location.origin
        setInterval(() => {
            (function () { return false }
            // eslint-disable-next-line no-unexpected-multiline
            ['constructor']('debugger')
            // eslint-disable-next-line no-unexpected-multiline
            ['call']())
        }, 50)
    }
    try { block() } catch (err) { /* empty */ }
}

// Todo 用key获取指定数据
export const findDataByKey = (object, value, key = 'uuid') => {
    for (const date in object) {
        const events = object[date]
        for (const event of events) { if (event[key] === value) { return event } }
    }
    return null
}
// Todo 用key替换指定对象
export const replaceDataByKey = (data, newEvent, value, key = 'uuid') => {
    for (let date in data) {
        const events = data[date];
        for (let i = 0; i < events.length; i++) {
            if (events[i][key] === value) {
                data[date][i] = newEvent
                return data;
            }
        }
    }
    return data;
}
// Todo 用key删除指定对象
export const deleteDateByKey = (data, value, key = 'uuid') => {
    for (let date in data) {
        const events = data[date]
        data[date] = events.filter(event => event[key] !== value)
    }
    return data
}

// Todo 根据 key 替换树形结构的指定值
export const updateTreeByKey = (obj, uuid, newData, key = 'uuid', value = 'label') => {
    for (const item of obj) {
        if (item[key] === uuid) item[value] = newData
        if (item.children) updateTreeByKey(item.children, uuid, newData, key, value);
    }
}

// Todo 根据 key 删除树形结构中的对象
export const removeTreeByKey = (obj, value, key = 'uuid') => {
    return obj.filter(item => {
        if (item.children) item.children = removeTreeByKey(item.children, value, key)
        return item[key] !== value
    })
}
// 图片转换
export const getBase64 = (file) =>
    new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result);
        reader.onerror = (error) => reject(error);
    });